package week03.resume.effic;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;
import java.util.Arrays;

/**
 * Created by zhangxiaokun on 2018/2/12.
 */
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private String[] addres;

    public User() {
    }

    public User(Integer id, String name, Integer age, String[] addres) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.addres = addres;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String[] getAddres() {
        return addres;
    }

    public void setAddres(String[] addres) {
        this.addres = addres;
    }

    public User clone01() {
        User user = new User();
        user.setId(new Integer(id));
        user.setName(new String(name));
        user.setAge(new Integer(age));
        user.setAddres(Arrays.copyOf(addres, addres.length));
        return user;
    }

    //通过系列化深度复制
    public User cloneBySerialize() {
        User user = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(this);

            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(in);
            user = (User) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return user;
    }

    //通过转json 解析，实现深度复制
    public User cloneByTranToJson() {
        String json = JSON.toJSONString(this);
        User user = JSON.parseObject(json, User.class);
        return user;
    }


}
